//
//  XLGHTTPManagerService.m
//  etionCBLogic
//
//  Created by wjxfhxy on 14-2-16.
//  Copyright (c) 2014年 GuangZhouXuanWu. All rights reserved.
//

#import "XLGHTTPManagerService.h"

#import "GWService+HTTPManagerService.h"

#import "XMSHttpChannel.h"

//#import "TransformHTTPRESTObj.h"

//#import "ModelBaseObj.h"

@implementation XLGHTTPManagerService

-(void)HTTPRESTRequestURI:(NSString*)URI extendHeadField:(NSDictionary*)headField dicData:(NSDictionary*)dic HTTPMethod:(XLGHTTPMethodType)nMethod completion:(void(^)(NSInteger nResult,id output,NSError *error))completion
{
    NSInteger nResult = NETWORKOFFLINE;
    NSDictionary *dicOutput = nil;
    NSError *error = nil;
    
    if([XLGContext context].m_bIsoffline == NO)
    {
        if([XLGContext context].m_bIsofflinelogin == NO)
        {
            nResult= [self.service HTTPRESTRequestURI:URI extendHeadField:headField dicData:dic HTTPMethod:nMethod output:&dicOutput protocolError:&error];
        }
        [dicOutput autorelease_mm];
        [error autorelease_mm];
    }
    
    completion(nResult,dicOutput,error);
}

-(XLGResult*)HTTPRESTRequestURI:(NSString*)URI extendHeadField:(NSDictionary*)headField dicData:(NSDictionary*)dic HTTPMethod:(XLGHTTPMethodType)nMethod
{
    XLGResult *r = nil;
    if([XLGContext context].m_bIsoffline == NO)
        r = [self.service HTTPRESTRequestURI:URI extendHeadField:headField dicData:dic HTTPMethod:nMethod];
    else
        r = [XLGResult resultWithOutResult:NETWORKOFFLINE outObj:nil outError:nil];
    
    return r;
}

-(void)HTTPRESTRequestURI:(NSString*)URI dicData:(NSDictionary*)dic HTTPMethod:(XLGHTTPMethodType)nMethod completion:(void(^)(NSInteger nResult,id output,NSError *error))completion
{
    [self HTTPRESTRequestURI:URI extendHeadField:nil dicData:dic HTTPMethod:nMethod completion:completion];
}

-(XLGResult*)HTTPRESTRequestURI:(NSString*)URI dicData:(NSDictionary*)dic HTTPMethod:(XLGHTTPMethodType)nMethod
{
    return [self HTTPRESTRequestURI:URI extendHeadField:nil dicData:dic HTTPMethod:nMethod];
}

/*
-(void)HTTPRESTRequestURI:(NSString*)URI requestKeyObj:(NSDictionary*)requestKeyObj objTransBlock:(void(NSString *key,id obj,id transObj))block HTTPMethod:(XLGHTTPMethodType)nMethod complete:(void(^)(NSInteger nResult,id output,NSError *error))complete
{
    if([XLGContext context].m_bIsoffline == NO)
    {
        NSDictionary*(^dicFun)(NSDictionary *dic)=NULL;
        NSArray*(^arFun)(NSArray* ar)=NULL;
        dicFun=^NSDictionary*(NSDictionary *dic)
        {
            NSMutableDictionary *dicObj = [[NSMutableDictionary new]autorelease_mm];
            [dic enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop)
             {
                 id transObj = obj;
                 if([obj isKindOfClass:[NSString class]]||[obj isKindOfClass:[NSNumber class]])
                     [dicObj setObject:obj forKey:key];
                 else if([[obj class] respondsToSelector:@selector(customObjToDictionary)])
                 {
                     transObj = [[obj class] customObjToDictionary:obj];
                     [dicObj setObject:transObj forKey:key];
                 }
                 else if([[obj class] respondsToSelector:@selector(protocolObjToDictionary:)])
                 {
                     transObj = [[obj class] protocolObjToDictionary:obj];
                     [dicObj setObject:transObj forKey:key];
                 }
                 else if([obj isKindOfClass:[NSArray class]])
                 {
                     transObj=arFun(obj);
                     [dicObj setObject:transObj forKey:key];
                 }
                 if(block!=NULL)
                 {
                     block(key,obj,transObj);
                 }
             }];
            return dicObj;
        };
        arFun=^NSArray*(NSArray* ar)
        {
            NSMutableArray *arObj=[[NSMutableArray new]autorelease_mm];
            [ar enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop)
            {
                [arObj addObject:(dicFun(obj))];
            }];
            return arObj;
        };
        
        NSDictionary *dic = dicFun(requestKeyObj);
        
        [self HTTPRESTRequestURI:URI dicData:dic HTTPMethod:nMethod complete:complete];
    }
    else
        complete(NETWORKOFFLINE,nil,nil);
}
*/

/*
-(void)HTTPRESTRequestURI:(NSString*)URI requestKeyObj:(NSDictionary*)requestKeyObj HTTPMethod:(XLGHTTPMethodType)nMethod complete:(void(^)(NSInteger nResult,id output,NSError *error))complete
{
    [self HTTPRESTRequestURI:URI requestKeyObj:requestKeyObj objTransBlock:NULL HTTPMethod:nMethod complete:complete];
}
*/

+(void)operateNeedTransferURLBlock:(NSString*(^)(NSString *URL))transferURL
{
    [XMSHttpChannel operateNeedTransferURLBlock:transferURL];
}

//extend
-(void)HTTPRESTRequestURI:(NSString*)URI protocol:(EProtocolTransformType)protocol extendHeadField:(NSDictionary*)headField dicData:(NSDictionary*)dic HTTPMethod:(XLGHTTPMethodType)nMethod completion:(void(^)(NSInteger nResult,id output,NSError *error))completion
{
    NSInteger nResult = NETWORKOFFLINE;
    NSDictionary *dicOutput = nil;
    NSError *error = nil;
    
    if([XLGContext context].m_bIsoffline == NO)
    {
        if([XLGContext context].m_bIsofflinelogin == NO)
        {
            nResult= [self.service HTTPRESTRequestURI:URI protocol:protocol extendHeadField:headField dicData:dic HTTPMethod:nMethod output:&dicOutput protocolError:&error];
        }
        [dicOutput autorelease_mm];
        [error autorelease_mm];
    }
    
    completion(nResult,dicOutput,error);
}

-(void)HTTPRESTRequestURI:(NSString*)URI protocol:(EProtocolTransformType)protocol dicData:(NSDictionary*)dic HTTPMethod:(XLGHTTPMethodType)nMethod completion:(void(^)(NSInteger nResult,id output,NSError *error))completion
{
    [self HTTPRESTRequestURI:URI protocol:protocol extendHeadField:nil dicData:dic HTTPMethod:nMethod completion:completion];
}
@end
